package io.jft.doll.weidget


import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.view.Gravity
import android.view.WindowManager
import chihane.jdaddressselector.AddressProvider
import chihane.jdaddressselector.AddressSelector
import chihane.jdaddressselector.OnAddressSelectedListener
import chihane.jdaddressselector.model.City
import chihane.jdaddressselector.model.County
import chihane.jdaddressselector.model.Province
import chihane.jdaddressselector.model.Street
import com.g.base.extend.dp
import com.g.base.extend.observeExOnce
import com.g.base.room.repository.Status
import com.g.base.ui.BaseActivity
import io.jft.doll.R
import io.jft.doll.room.repository.AddressRepository

class AddressSelectDialog : Dialog {
    private lateinit var selector: AddressSelector
    private lateinit var activity: BaseActivity<*>
    private val addressRepository by lazy { AddressRepository() }

    constructor(context: Context) : super(context, R.style.bottom_dialog) {
        init(context)
    }

    constructor(context: Context, themeResId: Int) : super(context, themeResId) {
        init(context)
    }

    constructor(context: Context, cancelable: Boolean, cancelListener: DialogInterface.OnCancelListener) : super(context, cancelable, cancelListener) {
        init(context)
    }

    private fun init(context: Context) {
        selector = AddressSelector(context)
        activity = context as BaseActivity<*>
        setContentView(selector.view)

        val window = window
        val params = window.attributes
        params.width = WindowManager.LayoutParams.MATCH_PARENT
        params.height = 256.dp()
        window.attributes = params
        window.setGravity(Gravity.BOTTOM)
    }

    fun setOnAddressSelectedListener(listener: OnAddressSelectedListener?) {
        if (listener == null) return
        this.selector.onAddressSelectedListener = listener

        selector.setAddressProvider(object : AddressProvider {
            override fun provideStreetsWith(p0: Int, p1: AddressProvider.AddressReceiver<Street>?) {
                p1?.send(null)
            }

            override fun provideCountiesWith(p0: Int, p1: AddressProvider.AddressReceiver<County>?) {
                addressRepository.getRegion(3.toString(), p0.toString())
                        .apply {
                            observeExOnce(activity) {
                                if (it.status == Status.Content) {
                                    val arrayList = ArrayList<County>()
                                    it.data?.forEach {
                                        arrayList.add(County().apply { name = it.name; id = it.id; city_id = p0 })
                                    }
                                    p1?.send(arrayList)
                                }
                            }
                        }
            }

            override fun provideCitiesWith(p0: Int, p1: AddressProvider.AddressReceiver<City>?) {
                addressRepository.getRegion(2.toString(), p0.toString())
                        .apply {
                            observeExOnce(activity) {
                                if (it.status == Status.Content) {
                                    val arrayList = ArrayList<City>()
                                    it.data?.forEach {
                                        arrayList.add(City().apply { name = it.name; id = it.id; province_id = p0 })
                                    }
                                    p1?.send(arrayList)
                                }
                            }
                        }
            }

            override fun provideProvinces(p0: AddressProvider.AddressReceiver<Province>?) {
                addressRepository.getRegion("1", "1")
                        .apply {
                            observeExOnce(activity) {
                                if (it.status == Status.Content) {
                                    val arrayList = ArrayList<Province>()
                                    it.data?.forEach {
                                        arrayList.add(Province().apply { name = it.name; id = it.id })
                                    }
                                    p0?.send(arrayList)
                                }
                            }
                        }
            }
        })
    }

    companion object {
        @JvmOverloads
        fun show(activity: Activity, listener: OnAddressSelectedListener? = null): AddressSelectDialog {
            val dialog = AddressSelectDialog(activity, R.style.bottom_dialog)
            dialog.setOnAddressSelectedListener(listener)
            dialog.show()
            return dialog
        }
    }
}